package com.ficus.road.maintain.third.zh.coverage;

import com.alibaba.fastjson.JSONArray;
import com.ficus.road.maintain.core.model.RoadCoverDetail;
import com.ficus.road.maintain.core.model.coverage.RoadCoverageWeek;
import com.ficus.road.maintain.core.model.coverage.RoadCoverageWeekZH;
import com.ficus.road.maintain.dao.coverage.RoadCoverageWeekDao;
import com.ficus.road.maintain.util.ZHTokenUtil;
import com.ficus.road.maintain.third.zh.BaseZH;
import com.google.common.base.Objects;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiawen yang
 * @date 2022/3/16 下午4:30
 */
@Component
@Slf4j
public class ZHRoadCoverageWeek extends BaseZH {

    // 子类不声明这个静态变量，下面的赋值语句会修改父类的静态变量的值
    private Integer connectTimeout;

    @Autowired
    private RoadCoverageWeekDao roadCoverageWeekDao;

    @Autowired
    private ZHTokenUtil zhTokenUtil;
    @Value("${zhonghai.ip}")
    private String ip;
    @Value("${zhonghai.webServicePort}")
    private int webServicePort;
    @Value("${zhonghai.username}")
    private String username;

    @Override
    public Integer getConnectTimeout() {
        return this.connectTimeout;
    }

    @PostConstruct
    void init() {
        arg0 = "ytkjRoadWeekCover";
        arg1 = "{}";
        successMsg = "调用中海接口获取道路本周期覆盖率成功！";
        errMsg = "调用中海接口获取道路本周期覆盖率异常！";
        this.connectTimeout = 300000;
    }

    //    @Scheduled(cron = "0/5 * * * * ?", zone = "GMT+8:00")
    @Scheduled(cron = "0 0/10 6-22 * * ?", zone = "GMT+8:00")
    public void refreshRoadCoverageWeek() {
        String roadCoverageStr = null;
        int i = 0;
        while (i < 10) {
            roadCoverageStr = getZHData();
            if (ObjectUtils.isNotEmpty(roadCoverageStr)) {
                break;
            }
            i++;
        }
        if (ObjectUtils.isEmpty(roadCoverageStr)) {
            return;
        }
        Date today = new Date();
        List<RoadCoverageWeekZH> roadCoverageWeekZHList = JSONArray.parseArray(roadCoverageStr, RoadCoverageWeekZH.class);
        List<RoadCoverageWeek> roadCoverageWeekList = roadCoverageWeekZHList.stream().map(item -> item.convert2RoadCoverage(item, today)).collect(Collectors.toList());
        roadCoverageWeekList.forEach(roadCoverageWeek -> {
            RoadCoverageWeek fetch = roadCoverageWeekDao.fetch(roadCoverageWeek.getId());
            if (ObjectUtils.isNotEmpty(fetch) && Objects.equal(fetch.getStatus(), RoadCoverDetail.RoadCoverageStatusEnum.AMENDMENT)) {
                roadCoverageWeek.setStatus(RoadCoverDetail.RoadCoverageStatusEnum.AMENDMENT);
            }
            roadCoverageWeekDao.saveOrUpdate(roadCoverageWeek);
        });
    }
}
